﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RibbonEditor.Controls
{
    public partial class EnumControl : UserControl, IDirtable
    {
        bool firstAccess = true;

        object initialSelectedItem = null;

        public bool IsDirty
        {
            set { if (value == false) this.initialSelectedItem = this.cboValue.SelectedItem; this.isDirty = value; }
            get { return this.isDirty; }
        }

        private bool isDirty;

        
        public string Label
        {
            get
            {
                return this.lblLabel.Text;
            }
            set
            {
                this.lblLabel.Text = value;
            }
        }

        public Array Values
        {
            set
            {
                this.cboValue.Items.Clear();

                this.cboValue.Items.Add("<no value selected>");

                foreach (object oValue in value)
                {
                    this.cboValue.Items.Add(oValue.ToString());
                }

                this.cboValue.SelectedIndex = 0;
            }
        }

        public string SelectedValue
        {
            get
            {
                if (this.cboValue.SelectedItem != null && this.cboValue.SelectedItem.ToString() != "<no value selected>")
                {
                    return this.cboValue.SelectedItem.ToString();
                }
                else
                {
                    return null;
                }
            }
            set
            {
                foreach (object o in this.cboValue.Items)
                {
                    if (value != null && o.ToString().ToLower() == value.ToLower())
                    {
                        if (!firstAccess)
                        {
                            if (this.initialSelectedItem == null)
                                this.initialSelectedItem = o;
                        }
                        else
                        {
                            firstAccess = false;
                        }

                        int index = this.cboValue.Items.IndexOf(o);
                        this.cboValue.SelectedIndex = index;
                    }
                }
            }
        }

        public bool IsValid()
        {
            if (this.IsRequired && this.SelectedValue == "<no value selected>")
                return false;

            return true;
        }

        public bool IsRequired
        {
            get
            {
                return this.lblRequired.Visible;
            }
            set
            {
                this.lblRequired.Visible = value;
            }
        }

        bool isEditable = true;

        public bool IsEditable
        {
            get { return isEditable; }
            set { isEditable = value; }
        }

        public EnumControl()
        {
            //this.cboValue.SelectedValueChanged -= new System.EventHandler(this.cboValue_SelectedValueChanged);
            
            InitializeComponent();

            this.cboValue.Enabled = isEditable;
            
            this.cboValue.SelectedValueChanged += new System.EventHandler(this.cboValue_SelectedValueChanged);
        }

        private void cboValue_SelectedValueChanged(object sender, EventArgs e)
        {
            this.IsDirty = (this.initialSelectedItem != this.cboValue.SelectedText);
        }

        public void ReinitValues()
        {
            this.cboValue.SelectedItem = this.initialSelectedItem;
            this.isDirty = false;
        }
    }
}
